arr =\
[[1,1,1,1,1],
 [1,0,0,0,1],
 [1,1,0,1,0],
 [0,1,1,0,1],
 [0,0,0,1,0]]



def dfs(arr,row,col):
    global tmp
    global count1
    if row<0 or col<0 or row==len(arr) or col==len(arr):
        return
    else:
        if arr[row][col]==1 or arr[row][col]==3:
            if arr[row][col]==1:
                count1=count1+1
                return
            else:
                return
        if arr[row][col]==0:
            arr[row][col]=3
            tmp.append([row,col])

        for i,j in [[1,0],[0,1],[-1,0],[0,-1]]:
            next_i,next_j = row+i,col+j
            dfs(arr,next_i,next_j)


#%%
res=[]
for i in range(len(arr)):
    for j in range(len(arr)):
        if arr[i][j]==0:
            count1 = 0
            tmp = []
            dfs(arr, row=i, col=j)
            if count1>=4 and count1%2==0:
                res+=tmp
for i in range(len(arr)):
    for j in range(len(arr)):
        if arr[i][j]==3:
            if [i,j] in res and (0 not in [i,j] and len(arr)-1 not in [i,j]):
                arr[i][j]=2
            else:
                arr[i][j]=0
arr